/**
 * 142. 环形链表 II
 * https://leetcode-cn.com/problems/linked-list-cycle-ii/
 */
const { createListNode } = require('../../libs/public');

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
function detectCycle(head) {
  if (!head) return null;
  let node = head;
  while (node) {
    if (node.checked) return node;
    node.checked = true;
    node = node.next;
  }
  return null;
}

let head;

head = createListNode([3, 2, 0, -4]);
head.next.next.next.next = head.next;
console.log(detectCycle(head).val === 2);

head = createListNode([1, 2]);
head.next.next = head;
console.log(detectCycle(head).val === 1);

head = createListNode([1]);
console.log(detectCycle(head) === null);
